package com.colabo.j2ee.web.core.log;

import java.io.Serializable;
import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.MethodBeforeAdvice;

import com.colabo.j2ee.web.core.common.CommonConstants;
import com.colabo.j2ee.web.core.util.PropertyUtil;

/**
 * 用于通过AOP方式拦截方法执行前的步骤，以便记入日志
 * 使用方法：<br>
 * 1：首先在xml配置文件中声明advise <br>
 * &lt;bean id="logbefore" class="com.cib.cap.base.common.log.LogBeforeAdvice"/&gt;<br>
 * 2:然后在xml配置文件中设置拦截的模式 <br>
 * &lt;aop:config &gt; 
 * 		&lt;aop:pointcut id="serviceOperation"  expression="execution(* com.cib.cap..*ServiceImpl.*(..))"/&gt;<br>
 * 		<aop:advisor pointcut-ref="serviceOperation" advice-ref="logbefore" /&gt;<br>
 * &lt;/aop:config&gt; <br>
 * 3:采用advisor方式，可以让log拦截和其它拦截器一起工作而互相不受干扰<br>
 */
public class LogBeforeAdvice implements MethodBeforeAdvice, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected final Log logger = LogFactory.getLog(LogBeforeAdvice.class);
	
	/**
	 * target的参数用来拦截后的控制，比如判断 target instanceof xxInterface
	 */
	public void before(Method method, Object[] args, Object target) {
		// 解析参数
		StringBuffer sb = new StringBuffer("");
		if (args != null) {
			for (int i = 0; i < args.length; i++) {
				if (args[i] != null) {
					sb.append(args[i].toString());
					sb.append(",");
				}
			}
		} else {
			sb.append("无输入参数");
		}
		logger.info(PropertyUtil.getPropertyByKey(CommonConstants.MSG_LOG_BEFORE, new String[] {
				target.getClass().getName(), method.getName(), sb.toString() }));
	}
}
